package com.wzh.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.wzh.mall.product.entity.AttrEntity;
import com.wzh.mall.product.service.AttrAttrgroupRelationService;
import com.wzh.mall.product.service.AttrService;
import com.wzh.mall.product.service.CategoryService;
import com.wzh.mall.product.vo.AttrGroupRelationVo;
import com.wzh.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wzh.mall.product.entity.AttrGroupEntity;
import com.wzh.mall.product.service.AttrGroupService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;



/**
 * 属性分组
 *
 * @author wzh
 * @email wzh3147255630@163.com
 * @date 2021-08-27 09:19:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
      * @Description: 新增关联关系
      * @Param:
      * @return:
      * @Author: Mr.Wang
      * @Date: 2021/9/4
      * @Time: 11:24
      */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vo){
        attrAttrgroupRelationService.saveBatch(vo);
        return R.ok();
    }

    /**
      * @Description: 查询没有分组关联的一些属性
      * @Param: [attrgroupId]
      * @return: com.wzh.common.utils.R
      * @Author: Mr.Wang
      * @Date: 2021/9/4
      * @Time: 10:31
      */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String,Object> params){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);

        return R.ok().put("page",page);
    }

    /**
      * @Description: 查询有分组关联的一些属性
      * @Param: [attrgroupId]
      * @return: com.wzh.common.utils.R
      * @Author: Mr.Wang
      * @Date: 2021/9/4
      * @Time: 10:33
      */

    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntities= attrService.getRelationAttr(attrgroupId);


        return R.ok().put("date",attrEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catalogoId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catalogoId")Long catalogoId){


//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page=attrGroupService.queryPage(params,catalogoId);

        return R.ok().put("page", page);
    }


    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        // 查出当前分类下的所有属性分组
        // 查出每个属性分组下的所有属性
        List<AttrGroupWithAttrsVo> vos=attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);


        return R.ok().put("data",vos);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		Long catelogId= attrGroup.getCatelogId();
		Long[] path=categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }


    /**
      * @Description: 删除
      * @Param: [vos]
      * @return: com.wzh.common.utils.R
      * @Author: Mr.Wang
      * @Date: 2021/9/4
      * @Time: 10:26
      */      
      
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){

        attrService.deleteRelation(vos);

        return R.ok();

    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
